/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.api;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.UserInfo;
import com.guoyu.project.service.UserInfoService;

/**
 * @author: libo Date: 17-6-28 Time: 上午10:16
 * @version: \$Id$
 */
@Controller
@RequestMapping("api/user/")
public class UserInfoApiController extends BaseApiController {

    @Resource
    private UserInfoService userInfoService;
    
    @RequestMapping("queryUserInfo")
    @ResponseBody
    public APIResponse queryUserInfoByid(int userId) {
    	UserInfo userInfo=userInfoService.queryById(userId);
    	return APIResponse.returnSuccess(userInfo);
    }
    
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public APIResponse update(UserInfo userInfo) {
    	userInfoService.updateUserInfo(userInfo);
    	return APIResponse.returnSuccess();
    }

    @RequestMapping("modifyPassword")
    @ResponseBody
    public APIResponse modifyPassword(String oldPassword, String newPassword, HttpServletRequest request) {
        UserInfo sessionUserInfo = getCurrentUser(request);
        if (sessionUserInfo == null) {
            return APIResponse.returnFail("用户未登录,请先登录");
        }
        // 判断原密码是否正确
        if (!StringUtils.equals(sessionUserInfo.getPassword(), DigestUtils.md5Hex(oldPassword))) {
            return APIResponse.returnFail("原密码错误");
        }
        userInfoService.modifyPassword(sessionUserInfo.getId(), newPassword);
        return APIResponse.returnSuccess();
    }
    @RequestMapping("getUserId")
    @ResponseBody
    public int getUserId(HttpServletRequest req){
    	UserInfo sessionUserInfo = getCurrentUser(req);
    	int userId = sessionUserInfo.getId();
    	return userId;
    }
}
